
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTMovable.h"

//to render the mark
#include "GRTGLVisu.h"

//to get opengl function
//#include <QGLWidget>
#include <QtOpenGL\qgl.h>

#include "RTMatrix44.h"
#include "GRTMemoryLeak.h"
//constructor
GRTMovable::GRTMovable():_selected(false), _hoover(0, 0, 0), _translate(0, 0, 0), _rotate(0, 0, 0), _scale(1, 1, 1)
	, _rotationMarkRadius(1), _translateMarkOffset(0), _movableType(MT_NONE){}

//destructor
GRTMovable::~GRTMovable(){}

//set the selected flag
void GRTMovable::setSelected(bool selected)
{
	_selected = selected;
}

//return the selected flag
bool GRTMovable::getSelected() const
{
	return _selected;
}

//toggle the selected flag
void GRTMovable::toggleSelected()
{
	_selected = !_selected;
}

//set the hoover flags
void GRTMovable::setHoover(unsigned int x, unsigned int y, unsigned int z)
{
	_hoover.x = x;
	_hoover.y = y;
	_hoover.z = z;
}

//set the hoover flags
void GRTMovable::setHoover(const RTVector3f& h)
{
	_hoover = h;
}

//pre render function
void GRTMovable::preRender()
{
	glPushMatrix();
}

//post render function
void GRTMovable::postRender()
{
	if(_selected)
		VISU.drawTranslationMark(_hoover);

	glPopMatrix();
}

//draw the geometry. This is the function you gotta call in your render loop
void GRTMovable::draw()
{
	preRender();
	Render();
	postRender();
}

//draw the translation mark of a sphere
void GRTMovable::drawTranslationMark()
{
	//reset the position of the sphere
	preRender();

	//set the position of the translation mark
	glTranslatef(_translateMark.x, _translateMark.y, _translateMark.z);

	//draw the mark
	VISU.drawTranslationMark(_hoover);

	//preRender does a pushMatrix so we do the popMatrix
	glPopMatrix();
}

//draw the rotation mark of the object
void GRTMovable::drawRotationMark()
{
	//X rotation
	glPushMatrix();
		//translate
		glTranslatef(_translate.x, _translate.y, _translate.z);
		
		//rotate
		glRotatef(_rotate.x, 1, 0, 0);

		//rotation for the x axis
		glScalef(_scale.x, _scale.y, _scale.z);

		glRotatef(90, 0, 1, 0);
		//draw mark x
		glColor3f(1, 0, 0);
		VISU.drawSimpleRotationMark(_hoover.x, _rotationMarkRadius);

	glPopMatrix();

	//Y rotation
	glPushMatrix();
		//translate
		glTranslatef(_translate.x, _translate.y, _translate.z);
		
		//rotate
		glRotatef(_rotate.x, 1, 0, 0);
		glRotatef(_rotate.y, 0, 1, 0);

		//rotation for the x axis
		glScalef(_scale.x, _scale.y, _scale.z);

		//draw mark
		glRotatef(90, 1, 0, 0);
		
		glColor3f(0, 1, 0);
		VISU.drawSimpleRotationMark(_hoover.y, _rotationMarkRadius);

	glPopMatrix();

	//Z rotation
	glPushMatrix();
		//translate
		glTranslatef(_translate.x, _translate.y, _translate.z);
		
		//rotate
		glRotatef(_rotate.x, 1, 0, 0);
		glRotatef(_rotate.y, 0, 1, 0);
		glRotatef(_rotate.z, 0, 0, 1);

		//rotation for the x axis
		glScalef(_scale.x, _scale.y, _scale.z);

		//draw mark 
		glColor3f(0, 0, 1);
		
		VISU.drawSimpleRotationMark(_hoover.z, _rotationMarkRadius);

	glPopMatrix();
}

//draw the scale mark of the object
void GRTMovable::drawScaleMark()
{
	//reset the position of the sphere
	preRender();

	//set the position of the translation mark
	//_translateMark = RTVector3f(0, _radius, 0);
	glTranslatef(_translateMark.x, _translateMark.y, _translateMark.z);

	//draw the mark
	VISU.drawScaleMark(_hoover);

	//preRender does a pushMatrix so we do the popMatrix
	glPopMatrix();
}

//get the translation vector
RTVector3f GRTMovable::getTranslate()const
{
	return _translate;
}

//get the rotation angles
RTVector3f GRTMovable::getRotate()const
{
	return _rotate;
}

//get the rotation matrix of the movable object
RTMatrix44 GRTMovable::getRotationMatrix()const
{
	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);

	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);

	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	return rZ * rY * rX;
}

//get the scaling factor
RTVector3f GRTMovable::getScale()const
{
	return _scale;
}

//get the movable type
MOVABLETYPE GRTMovable::getGRTMovableType()const
{
	return _movableType;
}

//set the translation vector
void GRTMovable::setTranslate(const RTVector3f& t)
{
	_translate = t;
}

//set the rotation angles
void GRTMovable::setRotation(const RTVector3f& r)
{
	_rotate = r;
}

//set the scaling factors
void GRTMovable::setScale(const RTVector3f& s)
{
	_scale = s;
}

//translate the object's position by the vector
void GRTMovable::translate(const RTVector3f& t)
{
	_translate = _translate + t;
}

//rotate the objects using the vector
void GRTMovable::rotate(const RTVector3f& r)
{
	_rotate = _rotate + r;
}

//add the vector as scale components
void GRTMovable::scale(const RTVector3f& s)
{
	_scale = _scale + s;
}


//test if the point v is on the mark of the primitive
bool GRTMovable::isTranslationMark(const RTVector3f& v)
{
	//calculate the matrix transform of the translation mark
	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform;
	transform = s * rZ * rY * rX * t;

	t.createTranslation(_translateMark);

	transform = t * transform;

	//get the world coorindates of the translation mark center
	RTVector3f center = RTVector3f(0, 0, 0) * transform;

	_hoover.x = 0;
	_hoover.y = 0;
	_hoover.z = 0;

	//get and test the distance between the point and the origin of the translation mark
	RTVector3f distance = v - center;
	if(distance.norme2() > 1)
		return false;

	//get the point in the local coordinates of the translation mark
	distance = v * transform.inverse();
	distance.normalize();

	//text with the tree axis.
	RTVector3f x(1, 0, 0);
	if(x.dot(distance) > 0.9f)
	{
		_hoover.x = 1;
		return true;
	}
	RTVector3f y(0, 1, 0);
	if(y.dot(distance) > 0.9f)
	{
		_hoover.y = 1;
		return true;
	}
	RTVector3f z(0, 0, 1);
	if(z.dot(distance) > 0.9f)
	{
		_hoover.z = 1;
		return true;
	}

	//v is not the mark
	return false;
}

//test if the point v is on the rotation mark of the primitive
//hoover indicate which axis is hoovered
bool GRTMovable::isRotationMark(const RTVector3f& v)
{
	
	//calculate the transformation matrix of the sphere
	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform;
	transform = s * rX * t;

	//float radius = _radius * sqrt(2.f); //!!!!!TO PUT IN A MEMBER!!!!!!!!!!
	float minRotationMarkRadius = _rotationMarkRadius - 0.2f;

	//preset to 0
	_hoover.x = 0;
	_hoover.y = 0;
	_hoover.z = 0;

	//X rotation
	RTVector3f localV = v * transform.inverse();
	float distance = localV.norme();
	if((localV.x < 0.2 && localV.x > -0.2) && (distance <_rotationMarkRadius && distance > minRotationMarkRadius))
	{
		_hoover.x = 1;
		return true;
	}

	//Y rotation
	transform = s * rY * rX * t;
	localV = v * transform.inverse();
	distance = localV.norme();
	if((localV.y < 0.2 && localV.y > -0.2) && (distance <_rotationMarkRadius && distance > minRotationMarkRadius))
	{
		_hoover.y = 1;
		return true;
	}

	//Z rotation
	transform = s * rZ * rY * rX * t;
	localV = v * transform.inverse();
	distance = localV.norme();
	if((localV.z < 0.2 && localV.z > -0.2) && (distance <_rotationMarkRadius && distance > minRotationMarkRadius))
	{
		_hoover.z = 1;
		return true;
	}

	//no mark hoovered
	return false;
}

//test if the point v is on the scale mark of the primitive
//hoover indicate which axis is hoovered
bool GRTMovable::isScaleMark(const RTVector3f& v)
{
	//calculate the matrix transform of the scale mark
	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform;
	transform = s * rZ * rY * rX * t;

	t.createTranslation(_translateMark);

	transform = t * transform;

	//get the world coordinates of the translation mark center
	//RTVector3f center = RTVector3f(0, 0, 0) * transform;

	//get and test the distance between the point and the origin of the translation mark
	//RTVector3f distance = v - center;

	//get the point in the local coordinates of the translation mark
	RTVector3f distance = v * transform.inverse();

	_hoover.x = 0;
	_hoover.y = 0;
	_hoover.z = 0;

	if(distance.norme2() > 4)
		return false;

	distance.normalize();

	//text with the tree axis.
	RTVector3f x(1, 0, 0);
	if(x.dot(distance) > 0.9f)
	{
		_hoover.x = 1;
		return true;
	}
	RTVector3f y(0, 1, 0);
	if(y.dot(distance) > 0.9f)
	{
		_hoover.y = 1;
		return true;
	}
	RTVector3f z(0, 0, 1);
	if(z.dot(distance) > 0.9f)
	{
		_hoover.z = 1;
		return true;
	}

	//v is not the mark
	return false;
}